﻿using SyncFileCommon.Model;
using System.ComponentModel;
using System.Data;

namespace SyncFileOption
{
    public partial class FrmDir : Form
    {
        /// <summary>
        /// 已添加的文件夹列表
        /// </summary>
        public List<SyncDirectoryModel> DirList;

        /// <summary>
        /// 用户列表
        /// </summary>
        public List<UserModel> AllUserList { get; set; }

        /// <summary>
        /// 可操作文件夹的用户列表
        /// </summary>
        public BindingList<UserModel> DirUserList { get; set; }


        /// <summary>
        /// 是否添加信息窗体
        /// </summary>
        public bool IsAdd { get; set; }

        public long ID;

        /// <summary>
        /// 共享文件夹路径
        /// </summary>
        public string DirPath
        {
            get
            {
                return txtDirPath.Text.Trim();
            }
            set
            {
                txtDirPath.Text = value;
            }
        }

        /// <summary>
        /// 可以访问共享文件夹的用户名列表，多个用户名之间以|分割，如果为空则为不限制
        /// </summary>
        public string UserList
        {
            get
            {
                if (chboxAllUser.Checked)
                {
                    return "[All]";
                }
                else
                {
                    return string.Join("|", DirUserList.Select(m => m.UserName).ToList());
                }
            }
            set
            {
                if (value == "[All]")
                {
                    chboxAllUser.Checked = true;
                }
                else
                {
                    string[] _userList = value.Split("|",StringSplitOptions.RemoveEmptyEntries);
                    DirUserList = new BindingList<UserModel>();

                    foreach (string _suser in _userList)
                    {
                        UserModel _user = AllUserList.First(m => m.UserName == _suser);
                        DirUserList.Add(_user);
                    }
                }
            }
        }



        public FrmDir()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;//设置边框为不可调节
            this.MaximizeBox = false; //取消最大化按键
            this.MinimizeBox = false; //取消最小化按键
            this.ShowInTaskbar = false;//不在任务栏显示多个窗体框
        }

        private void FrmDir_Load(object sender, EventArgs e)
        {
            cboxUserName.DropDownStyle = ComboBoxStyle.DropDownList;
            if (AllUserList != null)
            {
                cboxUserName.Items.Add("");
                foreach (UserModel _user in AllUserList)
                {
                    cboxUserName.Items.Add(_user.UserName);
                }
            }


            if (DirUserList == null)
            {
                DirUserList = new BindingList<UserModel>();
            }
            gridUser.MultiSelect = false;
            gridUser.AutoGenerateColumns = false;
            gridUser.AllowUserToAddRows = false;
            gridUser.DataSource = DirUserList;
        }

        private void btnClientSelectDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
            folderBrowser.SelectedPath = "";
            folderBrowser.Description = "请选择需要同步的文件夹";
            folderBrowser.ShowNewFolderButton = true;//是否显示新建文件夹按钮
            if (folderBrowser.ShowDialog() == DialogResult.OK)
            {
                this.txtDirPath.Text = folderBrowser.SelectedPath;
            }
        }

        private void chboxAllUser_CheckedChanged(object sender, EventArgs e)
        {
            if (chboxAllUser.Checked)
            {
                gridUser.Visible = false;
                cboxUserName.Visible = false;
                btnAddUser.Visible = false;
            }
            else
            {
                gridUser.Visible = true;
                cboxUserName.Visible = true;
                btnAddUser.Visible = true;
            }
        }

        private void btnAddUser_Click(object sender, EventArgs e)
        {
            if (cboxUserName.SelectedIndex < 1)
            {
                MessageBox.Show("请选择添加的用户");
                return;
            }

            if (DirUserList.FirstOrDefault(m => m.UserName == cboxUserName.Text) != null)
            {
                MessageBox.Show("用户已存在");
                return;
            }

            UserModel _user = AllUserList.Find(m => m.UserName == cboxUserName.Text);
            if (_user != null)
            {
                DirUserList.Add(_user);
            }
        }

        private void gridUser_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // 确认点击的是按钮列
            if (e.ColumnIndex == gridUser.Columns["colPassword"].Index && e.RowIndex >= 0)
            {
                // 执行按钮点击后的操作
                DirUserList.Remove(DirUserList.FirstOrDefault(m => m.UserName == gridUser.Rows[e.RowIndex].Cells["colUserName"].Value.ToString()));

            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (DirList != null)
            {
                if (DirList.Find(m => m.DirectoryPath == DirPath) != null)
                {
                    MessageBox.Show("共享文件夹已存在,无法重复添加");
                    return;
                }
            }
             
            this.DialogResult = DialogResult.OK;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
